package cn.quan.dlairport;

import java.text.DecimalFormat;
import java.util.Arrays;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.TextView;

public class SettingAdapter extends ArrayAdapter<String> {
	private List <String> mItems;
	private int mTextViewResourceId;
	private Context mContext;
	private LayoutInflater mInflater;
	//
	SharedPreferences mSetting;
	String mLocals;
	String[] mLocalArray;
	Editor mEditor;
	
	
	public SettingAdapter(Context context, int textViewResourceId,List<String> items) {
		super(context, textViewResourceId,items);
		// TODO Auto-generated constructor stub
		this.mItems = items;
		this.mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		this.mContext = context;
		this.mTextViewResourceId = textViewResourceId;
		
		makeSettingFilter();

	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		View v = convertView;
		if (v == null){
			v = mInflater.inflate(mTextViewResourceId, null);
		}else{
			//return v;
		}
		final String item=(String)mItems.get(position);
		final CheckBox checkbox1 = (CheckBox)v.findViewById(R.id.checkBox1);
		checkbox1.setText(item);
		if(containFilter(item)){
			checkbox1.setChecked(true);
		}else{
			checkbox1.setChecked(false);
		}
		checkbox1.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub

				if(checkbox1.isChecked()){//checked
            		checked(item);
				}else{
					unChecked(item);
				}
				
				
			}
		});

		return v;
	}
	private void checked(String item){
		
		String locals = this.mLocals;
		if(locals.length()>0){
			locals+=","+item;
		}else{
			locals=item;
		}
		commitSetting(locals);
		//Log.v(TAG,"check:"+locals);
		return;
	}
	private void unChecked(String item){
		
		String locals = "";
		for(int i=0;i<mLocalArray.length;i++){
			if(mLocalArray[i].compareTo(item)==0){
				
			}else{
				if(locals.length()>0){
					locals += ","+mLocalArray[i];
					
				}else{
					locals = mLocalArray[i];
					
				}
			}
		}
		commitSetting(locals);
		//Log.v(TAG,"uncheck:"+f);
		return;
	}
	
	private boolean containFilter(String item){
		if(Arrays.binarySearch(mLocalArray, item)>=0){
			return true;
		}else{
			return false;
		}
	}
	private void commitSetting(String locals){
		mEditor.putString(DlairportActivity.SETTING_FILTER, locals);
		mEditor.commit();
		mLocals = locals;
		mLocalArray = mLocals.split(",");
		Arrays.sort(mLocalArray);
		
	}
	private void makeSettingFilter(){
		mSetting = mContext.getSharedPreferences(DlairportActivity.SETTING,Activity.MODE_PRIVATE);
		mLocals = mSetting.getString(DlairportActivity.SETTING_FILTER, "");
		mLocalArray = mLocals.split(",");
		Arrays.sort(mLocalArray);

		mEditor = mSetting.edit();
		
	}

	private static boolean isCommit=false; 
	final String TAG = "**FLIGHT";

}
